package com.eclink.asset.hardware.server;

import com.eclink.asset.hardware.enums.HardwareServerStateEnum;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ServerHolder {

    public static Map<Integer, HardwareServer> serverMapping = new HashMap<>();

    public static void bindServer(int port) throws Exception {
        HardwareServer server = new HardwareServer();
        server.bind(port);
        serverMapping.put(port, server);
    }

    public static void activeServer(int port) throws Exception {
        HardwareServer server = serverMapping.get(port);
        server.setState(HardwareServerStateEnum.ACTIVE.getValue());
    }

    public static void inactiveServer(int port) throws Exception {
        HardwareServer server = serverMapping.get(port);
        server.setState(HardwareServerStateEnum.INACTIVE.getValue());
    }

    public static int getServerState(int port) {
        if (serverMapping.get(port) == null) {
            return HardwareServerStateEnum.INACTIVE.getValue();
        }
        return serverMapping.get(port).getState();
    }

    public static void saveServerMessage(int port, String message) {
        HardwareServer server = serverMapping.get(port);
        server.getMessageList().add(message);
    }

    public static List<String> getServerMessageList(int port) {
        HardwareServer server = serverMapping.get(port);
        return server.getMessageList();
    }

}
